<!DOCTYPE html>
<html>
	<head>
		<title>用户列表</title>
		<meta charset="utf-8">
		<meta name="viewport"
			content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<!-- 所有的 css & js 资源 -->
		<link rel="stylesheet" href="https://unpkg.com/element-ui@2.13.0/lib/theme-chalk/index.css">
		<link rel="stylesheet" href="../../static/sa.css">
		<script src="https://unpkg.com/vue@2.6.10/dist/vue.js"></script>
		<script src="https://unpkg.com/element-ui@2.13.0/lib/index.js"></script>
		<script src="https://unpkg.com/http-vue-loader@1.4.2/src/httpVueLoader.js"></script>
		<script src="https://unpkg.com/jquery@3.4.1/dist/jquery.min.js"></script>
		<script src="https://www.layuicdn.com/layer-v3.1.1/layer.js"></script>
		<script src="../../static/sa.js"></script>
		<style>
			.detail-box {
				padding: 10px;
				line-height: 2em;
			}
			.detail-box p {
				margin: 5px 0;
			}
			.detail-box b {
				font-size: 16px;
				color: #333;
			}
		</style>
	</head>
	<body>
		<div class="vue-box" style="display: none;" :style="'display: block;'">
			<div class="c-panel">
				<!-- ------------- 检索参数 ------------- -->
				<div class="c-title">用户列表</div>
				<el-form>
					<div class="c-item">
						<label class="c-label">用户昵称：</label>
						<el-input v-model="p.username" placeholder="模糊查询"></el-input>
					</div>
					<div class="c-item">
						<label class="c-label">注册日期：</label>
						<el-date-picker v-model="p.start_time" type="date" value-format="yyyy-MM-dd"
							placeholder="开始日期"></el-date-picker> -
						<el-date-picker v-model="p.end_time" type="date" value-format="yyyy-MM-dd"
							placeholder="结束日期"></el-date-picker>
					</div>
					<div class="c-item" style="min-width: 0px;">
						<el-button type="primary" icon="el-icon-search" @click="p.pageNo = 1; f5()">查询</el-button>
					</div>
					<br />
				</el-form>
				<!-- <div class="c-title">数据列表</div> -->
				<!-- ------------- 快捷按钮 ------------- -->
				<div class="fast-btn">
					<el-button type="primary" icon="el-icon-plus" @click="add()">新增</el-button>
					<el-button type="success" icon="el-icon-view" @click="getBySelect()">查看</el-button>
					<el-button type="danger" icon="el-icon-delete" @click="deleteByIds()">删除</el-button>
					<el-button type="warning" icon="el-icon-download" @click="sa.exportExcel()">导出</el-button>
					<el-button type="info" icon="el-icon-refresh" @click="sa.f5()">重置</el-button>
				</div>
				<!-- ------------- 数据列表 ------------- -->
				<el-table class="data-table" ref="data-table" :data="dataList" size="small">
					<el-table-column type="selection" width="45px"></el-table-column>
					
					<el-table-column label="头像" prop="image">
						<template slot-scope="s">
							<img :src="s.row.image ? baseImageUrl + s.row.image : baseImageUrl + 'default.png'" 
								@click="sa.showImage(s.row.image ? baseImageUrl + s.row.image : baseImageUrl + 'default.png', '400px', '400px')"
								style="width: 3em; height: 3em; float: left; margin-right: 1em; border-radius: 50%; cursor: pointer;" >
						</template>
					</el-table-column>
					<el-table-column label="昵称" prop="nickname"></el-table-column>
					<el-table-column label="角色" prop="role">
						<template slot-scope="s">
							<span v-if="s.row.role === 'ADMIN'">管理员</span>
							<span v-else-if="s.row.role === 'USER'">普通用户</span>
							<span v-else>{{s.row.role}}</span>
						</template>
					</el-table-column>
					<el-table-column label="性别" prop="sex"></el-table-column>
					<el-table-column label="手机号" prop="phone"></el-table-column>
					<el-table-column label="邮箱" prop="email"></el-table-column>
					
					<el-table-column label="注册时间" prop="createTime"></el-table-column>
					<el-table-column prop="address" label="操作">
						<template slot-scope="s">
							<el-button class="c-btn" type="success" icon="el-icon-view"
								@click="get(s.row)">详情</el-button>
							<el-button class="c-btn" type="danger" icon="el-icon-delete"
								@click="del(s.row)">删除</el-button>
						</template>
					</el-table-column>
				</el-table>
				<!-- ------------- 分页 ------------- -->
				<div class="page-box">
					<el-pagination background layout="total, prev, pager, next, sizes, jumper"
						:current-page.sync="p.pageNo" :page-size.sync="p.pageSize" :total="dataCount"
						:page-sizes="[1, 10, 20, 30, 40, 50, 100]" @current-change="f5(true)" @size-change="f5(true)">
					</el-pagination>
				</div>
			</div>
		</div>
		<script src="data-list.js"></script>
		<script type="text/javascript">
			var app = new Vue({
				el: '.vue-box',
				data: {
					p: { // 查询参数 
						pageNo: 1,
						pageSize: 10,
					},
					dataCount: 1422,
					dataList: [],
					baseImageUrl: 'http://8.134.184.130:8008/images/' // 图片服务器基础路径
				},
				methods: {
					// 数据刷新
					f5: function() {
						$.ajax({
							url: 'http://localhost:8080/user/listUser',
							type: 'GET',
							data: this.p,
							crossDomain: true,
							xhrFields: {
								withCredentials: true
							},
							headers: {
								'Accept': 'application/json',
								'Content-Type': 'application/json'
							},
							success: function(res) {
								console.log(res);
								if (res.code === 200) {
									// 使用 data.list 作为数据源
									this.dataList = res.data.list || [];
									// 计算总数
									this.dataCount = this.dataList.length;
									sa.f5TableHeight(); // 刷新表格高度 
								} else {
									console.error('请求失败:', res.message);
									sa.error('获取数据失败：' + res.message);
								}
							}.bind(this),
							error: function(xhr, status, error) {
								console.error('请求错误:', error);
								sa.error('获取数据失败，请确保后端服务已启动');
							}.bind(this)
						});
					},
					// 查看
					get: function(data) {
						var str = '<div class="detail-box">';
						str += '<p><b>基本信息</b></p>';
						str += '<p>ID：' + data.id + '</p>';
						str += '<p>头像：<img src="' + (this.baseImageUrl + data.image) + '" style="width: 50px; height: 50px; border-radius: 50%;"></p>';
						str += '<p>账号：' + data.username + '</p>';
						str += '<p>昵称：' + data.nickname + '</p>';
						str += '<p>角色：' + (data.role === 'ADMIN' ? '管理员' : '普通用户') + '</p>';
						str += '<p>性别：' + data.sex + '</p>';
						str += '<p>手机号：' + data.phone + '</p>';
						str += '<p>邮箱：' + data.email + '</p>';
						str += '<p>注册时间：' + data.createTime + '</p>';
						str += '</div>';
						sa.alert(str);
					},
					// 查看 - 根据选中的
					getBySelect: function(data) {
						var selection = this.$refs['data-table'].selection;
						if (selection.length == 0) {
							return sa.msg('请选择一条数据')
						}
						this.get(selection[0]);
					},
					// 新增
					add: function() {
						sa.showIframe('新增用户', 'user-add.html', '750px', '680px');
					},
					// 删除
					del: function(data) {
						sa.confirm('是否删除，此操作不可撤销', function() {
							$.ajax({
								url: 'http://localhost:8080/user/deleteUser',
								type: 'POST',
								data: {id: data.id},
								success: function(res) {
									if(res.code === 200) {
										// 从表格中移除该行数据
										var index = this.dataList.findIndex(item => item.id === data.id);
										if(index > -1) {
											this.dataList.splice(index, 1);
										}
										sa.ok('删除成功');
										this.f5(); // 刷新列表
									} else {
										sa.error(res.message || '删除失败');
									}
								}.bind(this),
								error: function(error) {
									console.error('删除失败:', error);
									sa.error('删除失败，请检查网络连接');
								}.bind(this)
							});
						}.bind(this));
					},
					// 批量删除
					deleteByIds: function() {
						// 获取选中元素的id列表
						let selection = this.$refs['data-table'].selection;
						let ids = sa.getArrayField(selection, 'id');
						if (selection.length == 0) {
							return sa.msg('请至少选择一条数据')
						}
						// 提交删除 
						sa.confirm('是否批量删除选中数据？此操作不可撤销', function() {
							sa.ajax2('/SysType/deleteByIds', {
								ids: ids.join(',')
							}, function(res) {
								sa.arrayDelete(this.dataList, selection);
								sa.ok('删除成功');
								sa.f5TableHeight(); // 刷新表格高度 
							}.bind(this))
						}.bind(this));
					},
				},
				created: function() {
					this.f5()
					sa.onInputEnter(); // 监听输入框的回车事件，执行查询  
				}
			})
		</script>
	</body>
</html>